package com.compunit.composite;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;

public class Box {

	private final float x;
	private final float y;
	private final float z;
	private final IntBuffer indexBuffer;
	private final FloatBuffer interleavedBuffer;
	
	private int maxIndex;
	private int indexCount;
	
	private int vertexVBO;
	private int normalVBO;
	private int indexVBO;
	private int interleavedVBO;
	
	public Box(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.indexBuffer = BufferUtils.createIntBuffer(14);
		this.interleavedBuffer = BufferUtils.createFloatBuffer(8 * 3 + 8 * 3 + 8 * 2);
		// Initialize vertices.
		final Vector3f[] vertices = new Vector3f[] {
				new Vector3f(-this.x, -this.y, -this.z),
				new Vector3f(this.x, -this.y, -this.z),
				new Vector3f(this.x, this.y, -this.z),
				new Vector3f(-this.x, this.y, -this.z),
				new Vector3f(this.x, -this.y, this.z),
				new Vector3f(-this.x, -this.y, this.z),
				new Vector3f(this.x, this.y, this.z),
				new Vector3f(-this.x, this.y, this.z),
		};
		this.buildIndices();
		this.buildInterleaved(vertices);
	}

	private void buildIndices() {
		final int[] indices = {2, 3, 6, 7, 5, 3, 0, 2, 1, 6, 4, 5, 1, 0};
		this.maxIndex = 7;
		this.indexCount = 14;
		this.indexBuffer.clear();
		this.indexBuffer.put(indices);
		this.indexBuffer.flip();
	}
	
	private void buildInterleaved(final Vector3f[] vertices) {
		this.interleavedBuffer.clear();
		for(int i = 0; i < vertices.length; i++) {
			final Vector3f normal = new Vector3f(vertices[i]).normalize();
			this.interleavedBuffer.put(vertices[i].x).put(vertices[i].y).put(vertices[i].z).put(normal.x).put(normal.y).put(normal.z);
			switch(i) {
			case 0: this.interleavedBuffer.put(1).put(0); break;
			case 1: this.interleavedBuffer.put(0).put(0); break;
			case 2: this.interleavedBuffer.put(0).put(1); break;
			case 3: this.interleavedBuffer.put(1).put(1); break;
			
			case 4: this.interleavedBuffer.put(1).put(0); break;
			case 5: this.interleavedBuffer.put(0).put(0); break;
			case 6: this.interleavedBuffer.put(1).put(1); break;
			case 7: this.interleavedBuffer.put(0).put(1); break;
			}
		}
		this.interleavedBuffer.flip();
	}
	
	public void setVertexVBO(int vertexVBO) {
		this.vertexVBO = vertexVBO;
	}
	
	public void setNormalVBO(int normalVBO) {
		this.normalVBO = normalVBO;
	}
	
	public void setIndexVBO(int indexVBO) {
		this.indexVBO = indexVBO;
	}
	
	public void setInterleavedVBO(int index) {
		this.interleavedVBO = index;
	}
	
	public IntBuffer getIndexBuffer() {
		return this.indexBuffer;
	}
	
	public FloatBuffer getInterleavedBuffer() {
		return this.interleavedBuffer;
	}
	
	public int getVertexVBO() {
		return this.vertexVBO;
	}
	
	public int getNormalVBO() {
		return this.normalVBO;
	}
	
	public int getIndexVBO() {
		return this.indexVBO;
	}
	
	public int getInterleavedVBO() {
		return this.interleavedVBO;
	}
	
	public int getMaxIndex() {
		return this.maxIndex;
	}
	
	public int getIndexCount() {
		return this.indexCount;
	}
}
